package com.example.zhujiajian.weixin;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.badgeview.Badge;
import q.rorbin.badgeview.QBadgeView;

public class MainActivity extends FragmentActivity {

    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mDatas;

    private TextView chatTextView;
    private TextView friendTextView;
    private  TextView contactTextView;

    private int tabline1_3;
    private ImageView mTablineIv;

    private LinearLayout mChatLl;

    private int curPageIdx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE );
        setContentView(R.layout.activity_main);

        mTablineIv = findViewById(R.id.id_iv_tabline);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);

        tabline1_3 = outMetrics.widthPixels / 3;

        ViewGroup.LayoutParams lp = mTablineIv.getLayoutParams();
        lp.width = tabline1_3;
        mTablineIv.setLayoutParams(lp);


        initView();
    }

    private void initView() {
        chatTextView = findViewById(R.id.id_text_chat);
        friendTextView = findViewById(R.id.id_text_friend);
        contactTextView = findViewById(R.id.id_text_contact);

        mChatLl = findViewById(R.id.id_ll_chat);

        mViewPager = findViewById(R.id.id_viewpager);
        mDatas = new ArrayList<>();
        ChatMainTabFragment tab01  = new ChatMainTabFragment();
        FriendMainTabFragment tab02 = new FriendMainTabFragment();
        ContactMainTabFragment tab03 = new ContactMainTabFragment();

        mDatas.add(tab01);
        mDatas.add(tab02);
        mDatas.add(tab03);

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public android.support.v4.app.Fragment getItem(int position) {
                return mDatas.get(position);
            }

            @Override
            public int getCount() {
                return mDatas.size();
            }
        };

        mViewPager.setAdapter(mAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.e("TAG",position + "," + positionOffset + "," + positionOffsetPixels);

                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTablineIv.getLayoutParams();
                if(curPageIdx == 0 && position == 0){//0->1
                    lp.leftMargin = (int) (positionOffset * tabline1_3 + curPageIdx * tabline1_3);
                }else if(curPageIdx == 1 && position == 0){//1->0
                    lp.leftMargin = (int) (curPageIdx * tabline1_3 + (positionOffset -1) * tabline1_3);
                }else if(curPageIdx == 1 && position == 1){//1->2
                    lp.leftMargin = (int) (curPageIdx * tabline1_3 + positionOffset * tabline1_3);
                }else if(curPageIdx == 2 && position == 1){//2->1
                    lp.leftMargin = (int) (curPageIdx * tabline1_3 + (positionOffset - 1) * tabline1_3);
                }
                mTablineIv.setLayoutParams(lp);
            }

            @Override
            public void onPageSelected(int position) {
                restTextView();

                switch (position){
                    case 0:
                        new QBadgeView(MainActivity.this).bindTarget(mChatLl).
                                setBadgeNumber(5).setOnDragStateChangedListener(new Badge.OnDragStateChangedListener() {
                            @Override
                            public void onDragStateChanged(int dragState, Badge badge, View targetView) {

                            }
                        });
                        chatTextView.setTextColor(Color.GREEN);
                        break;
                    case 1:
                        friendTextView.setTextColor(Color.GREEN);
                        break;
                    case 2:
                        contactTextView.setTextColor(Color.GREEN);
                        break;
                }

                curPageIdx = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void restTextView() {
        chatTextView.setTextColor(Color.BLACK);
        friendTextView.setTextColor(Color.BLACK);
        contactTextView.setTextColor(Color.BLACK);
    }
}
